﻿IF EXISTS (SELECT *  FROM sys.objects  WHERE object_id = OBJECT_ID(N'fn_DistributionMeter_GetNoteString') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
	DROP FUNCTION fn_DistributionMeter_GetNoteString

GO

CREATE FUNCTION [dbo].[fn_DistributionMeter_GetNoteString]
(
	  @DistributionMeterId int
)
RETURNS varchar(MAX)
AS
BEGIN

	DECLARE @Join varchar(MAX)
	SET @Join = ''
	
	SELECT
		@Join = @Join + Notes.[Name] + ' - '
	FROM 
		tb_Utility_Asset_Meters_Notes meterNotes
	INNER JOIN tb_Distribution_MeterNotes Notes
		ON Notes.MeterNoteId = meterNotes.Utility_Asset_MeterNoteFk
	WHERE
		meterNotes.Utility_Asset_MeterFk = @DistributionMeterId
	
	IF (@Join <> '' AND NOT @Join IS NULL)
		SET @Join = LEFT(@Join, LEN(@Join) - 2) 

	RETURN @Join

END







